package com.lagou.Client;

import com.lagou.Student.ClientAddStudent;
import com.lagou.Student.Student;
import com.lagou.Student.StudentList;
import com.lagou.Student.StudentMessage;
import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.io.IOException;
import java.util.*;

public class ClientView {
    private Student student;
    private ClientDao cd;
    private ClientInitClose cic;

    public ClientView(ClientInitClose cic,ClientDao cd) {
        this.cic = cic;
        this.cd=cd;
    }

    public void clientMainPage() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("    \t在线考试系统");
            System.out.println("__________________________________");
            System.out.print("   [1] 学员系统:");
            System.out.println("      [2] 管理员系统");
            System.out.println("   [0] 退出系统");
            System.out.println("__________________________________");
            System.out.println("请选择要进行的业务编号:");
            //Scanner sc = new Scanner(System.in);
            //int choose = sc.nextInt();
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    System.out.println("正在进入学员系统...");
                    break;
                case 2:
                    System.out.println("正在进入管理员系统...");
                    clientManagerLogin();
                    StudentList();
                    StudentAdd();
                case 0:
                    System.out.println("正在退出系统...");
                    return;//跳出循环
                default:
                    System.out.println("输入错误，请重新输入");
                    break;
            }
        }
    }

    private void clientManagerLogin() throws IOException, ClassNotFoundException {
        System.out.println("请输入管理员的账户信息:");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息:");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("managerCheck", new User(userName, password));
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送管理员信息成功!");
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())) {
            System.out.println("登录成功，欢迎使用!");
        } else {
            System.out.println("用户名或密码错误，请重试!");
        }
    }

    private void StudentList() {
        //准备一个List集合放Student的信息
        System.out.println("学员管理系统已打开...");
        List list = new LinkedList();
        list.add(new Student("沉鱼", "100000", "西施", 17, 1000));
        list.add(new Student("落雁", "100001", "王昭君", 18, 1001));
        list.add(new Student("闭月", "100002", "貂蝉", 19, 1002));
        list.add(new Student("羞花", "100003", "杨贵妃", 20, 1003));
        System.out.println("已有的学员的信息是:" + list);
    }

    private void StudentAdd() throws IOException, ClassNotFoundException, ClassCastException {
        List list = new LinkedList();
        list.add(new Student("沉鱼", "100000", "西施", 17, 1000));
        list.add(new Student("落雁", "100001", "王昭君", 18, 1001));
        list.add(new Student("闭月", "100002", "貂蝉", 19, 1002));
        list.add(new Student("羞花", "100003", "杨贵妃", 20, 1003));
        //添加学员信息并通过对象输出流发给服务器
        boolean flag = true;
        //List list1=new LinkedList();
        while (flag) {
            System.out.println("是否要添加学员信息?(true or false:)");
            String str1 = ClientScanner.getScanner().next();
            if ("true".equals(str1)) {
                System.out.println("请输入需要添加学员的用户名:");
                String studentUserName = ClientScanner.getScanner().next();
                System.out.println("请输入需要添加学员的密码:");
                String studentPassword = ClientScanner.getScanner().next();
                System.out.println("请输入需要添加学员的姓名:");
                String studentName = ClientScanner.getScanner().next();
                System.out.println("请输入需要添加学员的年龄:");
                int studentAge = ClientScanner.getScanner().nextInt();
                System.out.println("请输入需要添加学员的学号:");
                int studentNumber = ClientScanner.getScanner().nextInt();
                Student st = new Student(studentUserName, studentPassword, studentName, studentAge, studentNumber);
                cic.getOos().writeObject(st);
                list.add(st);
                System.out.println("客户端发送添加学员的信息成功!");
                System.out.println("添加学员的信息为:" + list);
            } else if ("false".equals(str1)) {
                String str = "false";
                cic.getOos().writeObject(str);
                System.out.println("添加学员信息完成!");
                //显示添加学员后，最新的学员信息
                System.out.println("添加学员后，最新的学员信息是:");
                System.out.println(list);
                flag = false;
            } else {
                System.out.println("输入有误,请重新输入哦!");
            }
        }


        List list2 = new LinkedList();
        System.out.println("请输入需要删除的学员信息的用户名:");
        String studentUserName = ClientScanner.getScanner().next();
        System.out.println("请输入需要删除的学员信息的密码，才可以进行删除操作:");
        String studentPassword = ClientScanner.getScanner().next();
        ClientAddStudent cas1 = new ClientAddStudent(studentUserName, studentPassword);
        cic.getOos().writeObject(cas1);
        System.out.println("客户端发送删除的学员信息成功!");
        list2.add(cas1);
        System.out.println("删除的学员信息是:" + list2);
        //接收服务器发回来的删除学员的信息
        //StudentMessage cm=(StudentMessage)cic.getOis().readObject();
        //if((cd.clientUserCheck(cm.getStudent()))&&(cd.clientUserCheck(cm.getStudent()))){
        list.remove(4);//删除liubei 100
        System.out.println("删除该学员后的全部学员信息是:" + list);
        System.out.println("正在给服务器发送删除该学员后的全部学员信息...");
        cic.getOos().writeObject(list);
        Object o = cic.getOis().readObject();
        if("success".equals(o)){
            System.out.println("服务器接收删除该学员后的全部学员信息成功!");
        }

        //修改下标位置的元素
        boolean flag1=true;
        while(flag1) {
            //System.out.println("[1] 用户名 [2] 密码 [3] 姓名 [4] 年龄 [5] 学号...");
            System.out.println("是否需要修改学员的信息:(true or false:)");
            String str2=ClientScanner.getScanner().next();
            if("true".equals(str2)) {
                System.out.println("请选择修改学员的信息的位置:");
                int ia = ClientScanner.getScanner().nextInt();
                //int choose=ClientScanner.getScanner().nextInt();
                //switch(choose){
                //case 1:
                //}
                Object pos1 = list.get(ia - 1);
                System.out.println("第" + ia + "个学员的信息是:" + pos1);
                System.out.println("下面请开始修改该学员的个人信息:");
                System.out.println("请输入用户名:");
                studentUserName = ClientScanner.getScanner().next();
                System.out.println("请输入密码:");
                studentPassword = ClientScanner.getScanner().next();
                System.out.println("请输入姓名:");
                String studentName = ClientScanner.getScanner().next();
                System.out.println("请输入年龄:");
                int studentAge = ClientScanner.getScanner().nextInt();
                System.out.println("请输入学号:");
                int studentNumber = ClientScanner.getScanner().nextInt();
                list.set(ia - 1, new Student(studentUserName, studentPassword, studentName, studentAge, studentNumber));
                System.out.println("修改后的全部学员信息是:" + list);
                System.out.println("正在给服务器发送修改后的学员信息...");
                Student st=new Student(studentUserName,studentPassword,studentName,studentAge,studentNumber);
                cic.getOos().writeObject(st);
                Object o1 = cic.getOis().readObject();
                if("success".equals(o1)){
                    System.out.println("服务器接收修改学员后的全部学员信息成功!");
                }
            }
            else if("false".equals(str2)){
                System.out.println("修改完毕!");
                flag1=false;
            }else{
                System.out.println("输入有误,请重新输入哦!");
            }
        }
        //正向查找
        boolean flag2=true;
        while (flag2) {
            System.out.println("是否需要查询学员的信息:(true or false:)");
            String str3=ClientScanner.getScanner().next();
            if("true".equals(str3)) {
                System.out.println("请输入需要查询的学员位置:");
                int ib = ClientScanner.getScanner().nextInt();
                Object pos = list.get(ib - 1);
                System.out.println("查询到的学员信息是:" + pos);
                System.out.println("正在给服务器发送查询后的学员信息...");
                cic.getOos().writeObject(pos);
                Object o2 = cic.getOis().readObject();
                if ("success".equals(o2)) {
                    System.out.println("服务器接收查询学员后的全部学员信息成功!");
                }
            }
            else if("false".equals(str3)){
                System.out.println("查询完毕!");
                flag2=false;
            }else{
                System.out.println("输入有误,请重新输入哦!");
            }
        }
    }
}